package com.ftg.midamall.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.midamall.admin.dto.UmsRoleDto;
import com.ftg.midamall.admin.entity.UmsRole;
import com.ftg.midamall.admin.mapper.UmsAdminRoleMapper;
import com.ftg.midamall.admin.mapper.UmsRoleMapper;
import com.ftg.midamall.admin.mapper.UmsRoleMenuMapper;
import com.ftg.midamall.admin.service.IUmsRoleService;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.RespMessUtil;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.Param;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2023-10-20
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements IUmsRoleService {

    @Resource
    UmsRoleMapper umsRoleMapper;

    @Resource
    UmsAdminRoleMapper umsAdminRoleMapper;

    @Resource
    UmsRoleMenuMapper umsRoleMenuMapper;

    /**
     * 添加角色以及分配菜单
     * 先判断角色名是否重复
     *
     * @param ur
     * @return
     */
    public RespMessUtil<Integer> insertRole(@Param("ur") UmsRoleDto ur, List<Long> menuIds) {

        UmsRoleDto n = umsRoleMapper.selectRoleName(ur.getName());
        if (!Objects.isNull(n)) {
            throw new RuntimeException("角色名不可重复");
        }

        int i = umsRoleMapper.insertRole(ur);

        Long role_id = umsRoleMapper.selectId(ur.getName());
        System.out.println(role_id);
        System.out.println(menuIds);

        if (i > 0) {
            for (Long menu_id : menuIds) {
                umsRoleMenuMapper.insertRoleMenuRelation(role_id, menu_id);
            }
        }

        return RespMessUtil.build(CodeStatusEnum.SUCCESS);
    }

    /**
     * 删除角色
     * 判断外键(找中间表)
     *
     * @param id
     * @return
     */

    public Integer deleteById(Long id) {
        UmsRole id1 = umsRoleMapper.selectByIdCount(id);
        if (Objects.isNull(id1)) {
            umsAdminRoleMapper.delRole(id);
            umsRoleMapper.deleteById(id);
            umsRoleMenuMapper.delRole(id);
        } else {
            throw new RuntimeException("该用户不存在/该角色底下有用户,你确定要删除吗");
        }
        return 1;

    }

    /**
     * 修改角色以及修改权限
     *
     * @param ur
     * @param
     * @return
     */
    public RespMessUtil<Integer> updateById(UmsRoleDto ur,List<Long> menuIds) {

        UmsRoleDto rename = umsRoleMapper.selectByName(ur.getName(), ur.getId());
        if (!Objects.isNull(rename)) {
            throw new RuntimeException("角色名不可重复");
        }
        Long role_id = ur.getId();
        umsRoleMapper.updateById(ur, role_id);

        umsRoleMenuMapper.selectMenuIdsByRoleId(role_id);

        umsRoleMenuMapper.delRole(role_id);

        for (Long menu_id : menuIds) {

            umsRoleMenuMapper.insertRoleMenuRelation(role_id, menu_id);
        }
        return RespMessUtil.build(CodeStatusEnum.SUCCESS);

    }

    /**
     * 分页展示
     *
     * @param pageSize
     * @param pageLimit
     * @param roleName
     * @return
     */

    @Override
    public RespMessUtil<IPage> showAllWithPage(long pageSize, long pageLimit, String roleName) {
        IPage<UmsRole> ipage = new Page<>(pageSize, pageLimit);
        QueryWrapper<UmsRole> query = new QueryWrapper<>();
        if (!StrUtil.isBlank(roleName)) {
            query.like("name", roleName);
        }
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, this.getBaseMapper().selectPage(ipage, query));
    }



    /**
     * 批量删除
     */
    @Override
    public Integer deleteBatchByIds(List<Long> ids) {
        return umsRoleMapper.deleteBatchIds(ids);

    }

    /**
     * 根据给定的id切换状态
     *
     * @param id 给定的id
     * @return 返回null
     */
    @Override
    public Boolean switchStatus(Long id) {
        return this.getBaseMapper().switchStatus(id);
    }

    @Override
    public RespMessUtil<List<Integer>> selectMeunIds(Long roleId) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS,umsRoleMenuMapper.selectMenuIdsByRoleId(roleId));
    }
}
